package li.com.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import li.com.ruiji.common.MyBaseContext;
import li.com.ruiji.common.R;
import li.com.ruiji.entity.ShoppingCart;
import li.com.ruiji.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
@Api("购物车相关操作")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
//    获得用户购物车信息
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }
//    向购物车添加信息
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        Long longId = MyBaseContext.getLongId();  //获得用户id(自己写的工具类)，和HttpSession.get得到的数据一样
        shoppingCart.setUserId(longId);
        Long dishId = shoppingCart.getDishId();  // 查看传过来的信息有没有dishId,来判断是套餐还是菜品
//        创建查询条件，查询购物车表中有没有该用户购买该菜品/套餐的信息，
//        如果有则把数量加一，如果没有则新建该信息
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,longId);
        if(dishId!=null){
//            是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
//            是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart shoppingCartOne = shoppingCartService.getOne(queryWrapper);
//        判断表中是否有该信息
        if(shoppingCartOne!=null){
            Integer number = shoppingCartOne.getNumber();
            shoppingCartOne.setNumber(number+1);
            shoppingCartService.updateById(shoppingCartOne);
        }else {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            shoppingCartOne=shoppingCart;
        }
        return R.success(shoppingCartOne);
    }
//    从购物车中删除信息
    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
//        创建查询条件，先判断用户id，在判断该信息是菜品还是套餐
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        Long longId = MyBaseContext.getLongId();
        queryWrapper.eq(ShoppingCart::getUserId,longId);
        if(shoppingCart.getDishId()!=null){
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
//        判断购物车中该信息的数量，如果大于 1 则减去1，否则直接删除
        ShoppingCart shoppingCartOne = shoppingCartService.getOne(queryWrapper);
        Integer number = shoppingCartOne.getNumber();
        if(number>1){
            shoppingCartOne.setNumber(number-1);
            shoppingCartService.updateById(shoppingCartOne);
        }else {
            shoppingCartService.remove(queryWrapper);
        }
        return R.success("取消成功");
    }
//    清空购物车
    @DeleteMapping("/clean")
    public R<String> clean(){
        Long longId = MyBaseContext.getLongId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,longId);
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }
}
